<style>
.center {
  margin-left: auto;
  margin-right: auto;
  font-size: 32px;
}
p {text-align: center;}
</style>

<?php

include 'functions.php';
$ID=$_GET["ID"];
$ShowUser=$_GET["User"];

if($ID!="")
{
$sql = "SELECT Name, Text, Options, Image FROM stor WHERE RefID=?";
$stmt = $conn->prepare($sql); 
$stmt->bind_param("i", $ID);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) 
  {
  // output data of each row
  while($row = $result->fetch_assoc()) 
	 {
		echo '<table class="center">
		<tr>
        <th>' . applyBB($row["Name"]) . ' </th>
		</tr>';
		if($row["Image"]!="0")
		{
		$imLink="images/0.jpg";
		$sql3 = "SELECT Link, Unlocked, Alt FROM Images WHERE ID=?";
		$stmt = $conn->prepare($sql3); 
		$into=$row["Image"];
		$stmt->bind_param("i", $into);
		$stmt->execute();
		$result3 = $stmt->get_result();
		$imDes="";
		while($row2 = $result3->fetch_assoc()) 
				{
				 if($row2["Unlocked"]=='1')
				 {
				 $imLink=$row2["Link"];
				 $imDes=$row2["Alt"];
				 }
			    }
		echo '<tr>
		<td><p></align><img src="' . $imLink .  '" alt="'. htmlentities($imDes) .'" style="width:400px;"></p></td>
		</tr>';
		}
		echo '<tr>
		<td>' . nl2br(applyBB($row["Text"])) . '</td>
		</tr>
		<tr>
		<td></td>
		</tr>';
		$Options=$row["Options"];
		if($Options!="")
		{
		$choices= explode(" ", $Options);
		$arrLength = count($choices);
		$l=1;
		for($i = 0; $i < $arrLength; $i++) 
			{
				
				
				$sql2 = "SELECT Description FROM stor WHERE RefID=?";
				$stmt = $conn->prepare($sql2); 
				$stmt->bind_param("i", $choices[$i]);
				$stmt->execute();
				$result2 = $stmt->get_result();
				 while($row2 = $result2->fetch_assoc()) 
				{
				if($row2["Description"]!="")
				{
				 echo "<tr><td><a href=index.php?ID=".$choices[$i]."> " . $l  . ": " ;
				 echo htmlentities($row2["Description"]);
				 echo "</a></td></tr> ";
				 $l=$l+1;
			    }
			    }
			   

			}
		}
		echo "</table>";
	 }
  } 
  else 
  {
  echo "Invalid ID";
  }
  }
  else
  {
	  if($ShowUser=='')
	  {
	  echo '<p style="font-size:32px; ">Available Stories: </p>';
		$sql = "SELECT ID, Title, Opener, Description, Owner FROM Stories ORDER BY rand()";
		$stmt = $conn->prepare($sql); 
		$stmt->execute();
		$result = $stmt->get_result();
		echo "<br>";
		if ($result->num_rows > 0) 
		  {
			    while($row = $result->fetch_assoc()) 
				{
					echo "<br><a href=index.php?ID=".$row["Opener"].">".htmlentities($row["Title"])."</a> <br> <i>".htmlentities($row["Description"])."</i>"  ;
					if($_SESSION['Level']>=$ModPermissionLevel)
					echo " <a href=index.php?Mod=6&ID=".$row["ID"].">Edit</a>";
					echo "<br>";
				}
		  }
	  }
	  else
	  {
		 if(is_numeric($ShowUser) && ($ShowUser==$_SESSION['ID'] || $_SESSION['Level']>=$ModPermissionLevel))
		 {
			echo "User ID " . $ShowUser . "<br><br> <b>Stories:</b> ";
			 $sql = "SELECT ID, Title, Opener, Description, Owner FROM Stories";
			 $stmt = $conn->prepare($sql); 
			 $stmt->execute();
		     $result = $stmt->get_result();
				echo "<br>";
				if ($result->num_rows > 0) 
				  {
						while($row = $result->fetch_assoc()) 
						{
							if($row["Owner"]==$ShowUser)
							echo "<br><a href=index.php?ID=".$row["Opener"].">".htmlentities($row["Title"])."</a> <br> <i>".htmlentities($row["Description"])."</i> - <a href=index.php?Mod=6&ID=".$row["ID"].">Edit</a><br>"  ;
						}
				  }
				  
			echo "<br> <b>Pages:</b> ";
			  $sql = "SELECT Name, Text, Owner, RefID FROM stor";
			 $stmt = $conn->prepare($sql); 
			 $stmt->execute();
		     $result = $stmt->get_result();
				echo "<br>";
				if ($result->num_rows > 0) 
				  {
						while($row = $result->fetch_assoc()) 
						{
							if($row["Owner"]==$ShowUser)
								echo "<br><a href=index.php?ID=".$row["RefID"].">".htmlentities($row["Name"])."</a> - <i>".htmlentities(substr($row["Text"], 0, 50))."...</i> - <a href=index.php?ID=".$row["RefID"]."&Mod=1>Edit</a>"  ;
						}
				  }
			
			echo "<br><br> <b>Images:</b> ";
			 $sql = "SELECT Link, Unlocked, Uploader, License, Alt, ID FROM Images";
			 $stmt = $conn->prepare($sql); 
			 $stmt->execute();
		     $result = $stmt->get_result();
				echo "<br>";
				if ($result->num_rows > 0) 
				  {
						while($row = $result->fetch_assoc()) 
						{
							if($row["Uploader"]==$ShowUser && $row["Unlocked"]=='1')
							echo '<br><img src="' . $row["Link"] .'" alt="'. htmlentities($row["Alt"]) .'" style="width:125;height:100px;"><br>ID:'.$row["ID"].' License: '.htmlentities($row["License"]).' Alt Text: '.$row["Alt"].'<br>';
						}
				  }
		 } 
	  }
  }
?> 
